/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Army;

import Army.building.Building;
import Army.building.BuildingFactory;
import Army.unit.UnitFactory;
import Army.unit.Unit;
import MOM.Message;
import MOM.Subcriber;
import java.awt.Graphics2D;
import java.awt.Point;
import java.util.ArrayList;

/**
 *
 * @author Alvis
 */
public class PlayerTeam implements Subcriber {

	public static int LIMIT = 0;
	int groupNumber;
	int teamId;
	int populationLimit = 10;
	int gold = 0;
	Building castle;
	ArrayList<Building> listBuilding;
	ArrayList<Unit> listUnit;

	public int getTeamId() {
		return teamId;
	}

	public Building getCastle() {
		return castle;
	}

	public Point getPosition() {
		return castle.getPosition();
	}

	public int getPopulationLimit() {
		return populationLimit;
	}

	public boolean isIsControled() {
		return true;
	}

	public void setPopulationLimit(int populationLimit) {
		this.populationLimit = populationLimit;
	}

	public int getGold() {
		return gold;
	}

	public void setGold(int gold) {
		this.gold = gold;
	}

	public int getPopulation() {
		return listUnit.size();
	}

	public ArrayList<Building> getListBuilding() {
		return listBuilding;
	}

	public ArrayList<Unit> getListUnit() {
		return listUnit;
	}

	public boolean isAlive() {
		return castle.isAlive();
	}

	public boolean add(Building building) {
		if (building == null || gold < building.getBuildCost()) {
			return false;
		}
		gold -= building.getBuildCost();
		listBuilding.add(building);
		return true;
	}

	protected boolean add(Unit unit) {
		listUnit.add(unit);
		return true;
	}

	public int getGroupNumber() {
		return groupNumber;
	}

	public void setGroupNumber(int number) {

		for (int i = 0; i < listUnit.size(); i++) {
			if (listUnit.get(i).getGroupNumber() == number) {
				listUnit.get(i).setGroupNumber(0);
			}
			if (listUnit.get(i).isIsSelected() == true) {


				listUnit.get(i).setGroupNumber(number);
			}
		}
	}

	public PlayerTeam(int team, int x, int y) {
		teamId = team;
		listBuilding = new ArrayList<>();
		listUnit = new ArrayList<>();
		gold = Integer.MAX_VALUE;
		createNewBuilding("castle", x, y);
		castle = listBuilding.get(0);
	}

	public Building createNewBuilding(String type, int x, int y) {
		BuildingFactory factory;
		factory = new BuildingFactory();
		Building tower = null;
		tower = factory.createBuilding(type, teamId, x, y);

		if (add(tower)) {
			return tower;

		}
		return null;

	}

	public Unit createNewUnit(String type, Building b) {

		UnitFactory unitFactory;
		unitFactory = new UnitFactory();
		Unit unit = null;
		unit = unitFactory.createUnit(type, teamId, 0, 0);
		unit.startBuild();
		if (unit == null || gold < unit.getBuildCost() || getPopulation() >= populationLimit) {
			return null;
		} else if (b.addUnit(unit)) {
			gold -= unit.getBuildCost();
			return unit;
		}
		return null;
	}

	public void update() {
		for (int i = 0; i < listBuilding.size(); i++) {
			Building building = listBuilding.get(i);
			if (building.isIsDetroyed()) {
				removeBuilding(i);
				i--;
			} else {
				building.update();
				if (getPopulation() < populationLimit) {
					building.train();
				}
			}
		}
		for (int i = 0; i < listUnit.size(); i++) {
			Unit unit = listUnit.get(i);
			if (unit.isIsDetroyed()) {
				removeUnit(i);
				i--;
			} else {
				unit.update();
			}

		}

	}

	public void draw(Graphics2D g, int dx, int dy) {


		for (int i = 0; i < listBuilding.size(); i++) {
			Building building = listBuilding.get(i);
			building.draw(g, dx, dy);
		}
		for (int i = 0; i < listUnit.size(); i++) {
			Unit unit = listUnit.get(i);
			unit.draw(g, dx, dy);
		}
	}

	public void getSelectGroup(int number) {
		for (int i = 0; i < listUnit.size(); i++) {
			if (listUnit.get(i).getGroupNumber() == number) {
				listUnit.get(i).select();
			}
		}

	}

	protected Unit removeUnit(int i) {
		return listUnit.remove(i);
	}

	protected Building removeBuilding(int i) {
		Building building = listBuilding.remove(i);
		if (building.getType() == BuildingAndUnit.Type.BuildingHouse) {
			populationLimit -= 5;
		}
		return building;
	}

	/*protected void remove(BuildingAndUnit unit) {
	 if (unit.isUnit()) {
	 listUnit.remove(unit);
	 } else {
	 listBuilding.remove(unit);
	 if (unit.getType() == BuildingAndUnit.Type.BuildingHouse) {
	 populationLimit -= 5;
	 }
	 }
	 }*/
	@Override
	public void notify(Message msg) {
		if (!isAlive()) {
			return;
		}
		if (msg.message.equalsIgnoreCase("waged")) {
			gold += (int) msg.object;
		} else if (msg.message.equalsIgnoreCase("add")) {
			Unit unit = (Unit) msg.object;
			add(unit);

		} else if (msg.message.equalsIgnoreCase("completed")) {
			if (((BuildingAndUnit) msg.object).getType() == BuildingAndUnit.Type.BuildingHouse) {

				populationLimit += 5;
				if (populationLimit > LIMIT) {
					populationLimit = LIMIT;
				}
			}
		}
	}
}
